package com.gmlive.common.sample;

import android.content.Context;
import android.text.format.DateFormat;
import android.text.format.DateUtils;

import java.util.Calendar;

/**
 * @Author Sven
 * @Date 2021/5/26
 * @Email jiangzhimin@inke.cn
 */
public class DateFormatUtils {
    private DateFormatUtils() {
    }

    public static String formatMessageTime(Context context, long millis) {
        int formatFlags = DateUtils.FORMAT_SHOW_TIME;
        if (DateFormat.is24HourFormat(context)) {
            formatFlags |= DateUtils.FORMAT_24HOUR;
        } else {
            formatFlags |= DateUtils.FORMAT_12HOUR;
        }

        if (!DateUtils.isToday(millis)) {
            Calendar today = Calendar.getInstance();
            Calendar other = Calendar.getInstance();
            other.setTimeInMillis(millis);

            formatFlags |= DateUtils.FORMAT_SHOW_DATE;

            if (today.get(Calendar.YEAR) != other.get(Calendar.YEAR)) {
                formatFlags |= DateUtils.FORMAT_SHOW_YEAR;
            }
        }

        return DateUtils.formatDateTime(context, millis, formatFlags);
    }
}
